package com.boot.web.controller;

import java.util.Date;
import java.util.Map;

import com.boot.web.dao.CustomerDao;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.boot.communal.paging.InitPaging;
import com.boot.communal.response.ResponseServer;
import com.boot.web.entity.Customer;
import com.boot.web.service.CustomerService;

/**
 * 客户信息控制器
 * @author 【zhouJie】
 * @date 2021-06-02 08:43:22
 */
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private CustomerDao customerDao;

    @PostMapping("/save")
    public ResponseServer save(Customer customer) {
        customer.setUpdateTime(new Date());
        return ResponseServer.success("添加成功", customerService.save(customer));
    }

    @PutMapping("/update")
    public ResponseServer update(Customer customer) {
        Customer c = customerService.getById(customer.getId());
        if(c.getMobile() == null){

        }else{
            if(!c.getMobile().equals(customer.getMobile())){
                if(customerDao.queryCustomerByMobile(customer.getMobile()) != null){
                    return ResponseServer.error("手机号码已存在");

                }
            }
        }
        if(customer.getAgentCode() == null){

        }else{
            if(c.getAgentCode() == null){

            }else {
                if(!c.getAgentCode().equals(customer.getAgentCode())){
                    if(customerDao.queryCustomerByAgentCode(customer.getAgentCode()) != null){
                        return ResponseServer.error("用户编号已存在");
                    }
                }
            }
        }

        if(c.getParentId().equals(customer.getParentId())){

        }else{
            Customer p = customerService.getById(customer.getParentId());
            if(p == null){
                return ResponseServer.error("上级ID不存在");
            }
            if(p.getAgentCode() != null && !"".equals(p.getAgentCode())){
                String str = p.getAgentCode().substring(0,3);
                if(c.getAgentCode() != null && !"".equals(c.getAgentCode())){

                }else{
                    String newCode = "";
                    do {
                        newCode = str + randoms();
                        if (customerDao.queryCustomerByAgentCode(newCode) == null) {
                            break;
                        }
                    } while (customerDao.queryCustomerByAgentCode(newCode) != null);
                    customer.setAgentCode(newCode);
                }
            }else{
                customer.setAgentCode("");
            }
        }

        if(!c.getUnionId().equals(customer.getUnionId())){
            if(customerDao.queryCustomerByUnionId(customer.getUnionId()) != null){
                return ResponseServer.error("微信UnionId不存在");
            }
        }

        customerService.update(customer);
        return ResponseServer.success("修改成功");
    }

    @PutMapping("/update1")
    public ResponseServer update1(Customer customer) {
        if(!"2".equals(customer.getVerifyStatus())){
            customer.setFailedInfo("");
        }
        customerService.update(customer);
        return ResponseServer.success("修改成功");
    }

    public static int randoms() {
        return 1 + (int) (Math.random() * 1000000);
    }

    @DeleteMapping("/delete/{id}")
    public ResponseServer delete(@PathVariable Integer id) {
        Customer customer = customerService.getById(id);
        // 假删除
        customer.setIsDel("1");
        customerService.update(customer);
        return ResponseServer.success("删除成功");
    }

    @DeleteMapping("/batchDelete/{ids}")
    public ResponseServer batchDelete(@PathVariable Integer[] ids) {
        if(ids.length > 0){
            for(int i = 0; i < ids.length; i++){
                Customer customer = customerService.getById(ids[i]);
                // 假删除
                customer.setIsDel("1");
                customerService.update(customer);
            }
        }
        return ResponseServer.success("批量删除成功");
    }

    @GetMapping("/getPageList")
    public ResponseServer getPageList(@RequestParam Map<String, Object> params) {
        InitPaging.init(params);
        return ResponseServer.success(new PageInfo(customerService.getCustomerVOPageList(params)));
    }

    @GetMapping("/getAll")
    public ResponseServer getAll() {
        return ResponseServer.success(customerService.getAllVO());
    }

    @GetMapping("/getById/{id}")
    public ResponseServer getById(@PathVariable Integer id) {
        return ResponseServer.success(customerService.getById(id));
    }

}
